package cn.hbwe.service.impl;

import cn.hbwe.mapper.DepartmentMapper;
import cn.hbwe.mapper.DoctorMapper;
import cn.hbwe.pojo.entity.*;
import cn.hbwe.mapper.ScheduleMapper;
import cn.hbwe.pojo.vo.DoctorVo;
import cn.hbwe.pojo.vo.ScheduleVo;
import cn.hbwe.pojo.vo.dScheduleVo;
import cn.hbwe.service.IDepartmentService;
import cn.hbwe.service.IDoctorService;
import cn.hbwe.service.IScheduleService;
import cn.hbwe.util.PageVO;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;




/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 7组
 * @since 2023-10-18
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements IScheduleService {

   @Autowired
   private DepartmentMapper departmentMapper;

    @Autowired
    private DoctorMapper doctorMapper;



    @Autowired
    private ScheduleMapper scheduleMapper;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IDoctorService doctorService;



    @Override
    public PageVO<ScheduleVo> getScheduleVo(Long currentPage, Long pageSize, Integer id) {
        Page<Schedule> page = Page.of(currentPage, pageSize);
        LambdaQueryWrapper<Schedule> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, Schedule::getDepartmentId, id);

        LocalDateTime now = LocalDateTime.now(); // 获取当前时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // 定义格式
        String formattedNow = now.format(formatter); // 格式化时间
        lqw.ge(Schedule::getScheduleDate, formattedNow);




        Page<Schedule> doctorPage = page(page, lqw);

        long total = doctorPage.getTotal();
        long current = doctorPage.getCurrent();
        List<Schedule> doctors = doctorPage.getRecords();
        if (doctors == null) {
            return PageVO.of(total, current, Collections.emptyList());
        }

        List<ScheduleVo> doctorVos = BeanUtil.copyToList(doctors, ScheduleVo.class);
        for (Schedule doctor : doctors) {
            Integer departmentId = doctor.getDepartmentId();
            Department department = departmentMapper.selectById(departmentId);

            for (ScheduleVo doctorVo : doctorVos) {
                if (departmentId == doctorVo.getDepartmentId()){
                    doctorVo.setDepartmentName(department.getDepartmentName());

                }
            }
        }


        for (Schedule doctor : doctors) {
            Integer doctorId = doctor.getDoctorId();
            Doctor doctor1 = doctorMapper.selectById(doctorId);



            for (ScheduleVo doctorVo : doctorVos) {
                if (doctorId == doctorVo.getDoctorId()){
                    doctorVo.setDoctorNo(doctor1.getDoctorNo());
                    doctorVo.setPrice(doctor1.getPrice());
                    doctorVo.setDoctorSex(doctor1.getDoctorSex());

                }
            }
        }

        return PageVO.of(total, current,doctorVos);
    }



    @Override
    public PageVO<ScheduleVo> getByTime(Long currentPage, Long pageSize, String time) {
        Page<Schedule> page = Page.of(currentPage, pageSize);
        LambdaQueryWrapper<Schedule> lqw = new LambdaQueryWrapper<>();
        lqw.like(time != null, Schedule::getScheduleDate, time);
        Page<Schedule> schedulePage = page(page, lqw);

        long total = schedulePage.getTotal();
        long current = schedulePage.getCurrent();
        List<Schedule> schedules = schedulePage.getRecords();
        if (schedules == null) {
            return PageVO.of(total, current, Collections.emptyList());
        }

        List<ScheduleVo> scheduleVos = BeanUtil.copyToList(schedules, ScheduleVo.class);

        for (Schedule schedule : schedules) {
            Integer departmentId = schedule.getDepartmentId();
            Department department = departmentService.getById(departmentId);
            Integer doctorId = schedule.getDoctorId();
            Doctor doctor = doctorService.getById(doctorId);

            for (ScheduleVo scheduleVo : scheduleVos) {
                if (departmentId == scheduleVo.getDepartmentId()) {
                    scheduleVo.setDepartmentName(department.getDepartmentName());
                }
                if (doctorId == scheduleVo.getDoctorId()) {
                    scheduleVo.setDoctorNo(doctor.getDoctorNo());
                    scheduleVo.setDoctorSex(doctor.getDoctorSex());
                    scheduleVo.setPrice(doctor.getPrice());
                }
            }
        }
        return PageVO.of(total, current, scheduleVos);
    }


    @Override
    public List<Schedule> getScheduleId(int id) {
        LambdaQueryWrapper<Schedule> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Schedule::getScheduleId,id);
        List<Schedule> bannerItems = scheduleMapper.selectList(lqw);
        return  bannerItems;
    }

    //------------yuhagyu------------
    /**
     * 根据医生id查看排班信息
     * 仅查看当天时间以后的
     */
    @Override
    public PageVO<dScheduleVo> getByDoctorId(Long currentPage, Long pageSize, Integer doctorId) {
        Page<Schedule> page = Page.of(currentPage, pageSize);
        LocalDateTime now = LocalDateTime.now(); // 获取当前时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 定义格式
        String formattedNow = now.format(formatter); // 格式化时间

        // 切割字符串，获取年月日时分秒六个字符串
        String year = formattedNow.substring(0, 4);
        String month = formattedNow.substring(5, 7);
        String day = formattedNow.substring(8, 10);
        String hour = formattedNow.substring(11, 13);
        String minute = formattedNow.substring(14, 16);
        String second = formattedNow.substring(17, 19);

        LocalDateTime afterTime = LocalDateTime.of(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day), Integer.valueOf(0), Integer.valueOf(0),Integer.valueOf(0));

        LambdaQueryWrapper<Schedule> lqw = new LambdaQueryWrapper<>();
        lqw.ge(Schedule::getScheduleDate, afterTime); // 设置开始时间晚于指定时间点的条件
        lqw.eq(doctorId != null, Schedule::getDoctorId, doctorId);
        Page<Schedule> schedulePage = page(page, lqw);
        long total=schedulePage.getTotal();
        long current=schedulePage.getCurrent();
        List<Schedule> schedules=schedulePage.getRecords();
        if(schedules==null){
            return PageVO.of(total,current, Collections.emptyList());
        }
        List<dScheduleVo> scheduleVos=BeanUtil.copyToList(schedules,dScheduleVo.class);
        System.out.println(doctorId);
        Doctor doctor=doctorService.selectByOneself(doctorId);
        for(dScheduleVo scheduleVo : scheduleVos){
            scheduleVo.setDoctorNo(doctor.getDoctorNo());
        }

        return PageVO.of(total,current,scheduleVos);
    }



}
